<?php

namespace app\admin\controller\scanwork;

use app\common\controller\Backend;
use think\Db;
use Exception;

/**
 * 工艺路线管理
 *
 * @icon fa fa-road
 * @remark 管理产品工艺路线和工序流程
 */
class ProcessRoute extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\scanwork\ProcessRoute;
        $this->view->assign("routeTypeList", $this->model->getRouteTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['product', 'model', 'creator'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            return json(["total" => $list->total(), "rows" => $list->items()]);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if (empty($params['route_code'])) {
                    $params['route_code'] = \app\admin\model\scanwork\ProcessRoute::generateRouteCode();
                }
                $params['creator_id'] = $this->auth->id;
                $params['creator_name'] = $this->auth->username;
                
                Db::startTrans();
                try {
                    $this->model->save($params);
                    Db::commit();
                    $this->success();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
            }
        }
        
        $productList = \app\admin\model\scanwork\Product::where('status', 1)->column('name', 'id');
        $this->view->assign('productList', $productList);
        return $this->view->fetch();
    }

    /**
     * 工艺步骤管理
     */
    public function steps($ids = null)
    {
        $route = $this->model->get($ids);
        if (!$route) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isAjax()) {
            $steps = \app\admin\model\scanwork\ProcessRouteStep::where('route_id', $ids)
                ->with(['process', 'workCenter'])
                ->order('sequence', 'asc')
                ->select();

            return json(['code' => 1, 'data' => $steps]);
        }

        $processList = \app\admin\model\scanwork\Process::where('status', 1)->column('name', 'id');
        $workCenterList = \app\admin\model\scanwork\WorkCenter::where('status', 1)->column('center_name', 'id');
        
        $this->view->assign('processList', $processList);
        $this->view->assign('workCenterList', $workCenterList);
        $this->view->assign('route', $route);
        return $this->view->fetch();
    }

    /**
     * 获取流程图数据
     */
    public function flowChart($ids = null)
    {
        $route = $this->model->get($ids);
        if (!$route) {
            $this->error(__('No Results were found'));
        }

        $flowData = $route->getFlowChart();
        return json(['code' => 1, 'data' => $flowData]);
    }

    /**
     * 复制工艺路线
     */
    public function copy($ids = null)
    {
        $route = $this->model->get($ids);
        if (!$route) {
            $this->error(__('No Results were found'));
        }

        Db::startTrans();
        try {
            $newVersion = $this->request->post('new_version');
            $newRoute = $route->copyRoute($newVersion);
            
            Db::commit();
            $this->success('复制成功', null, ['id' => $newRoute->id]);
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }
}

